<?
include_once("database.php");
include_once("backend.php");
include_once("login_management.php");
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/Scouting.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Edit Match | Team Spyder Scouting</title>
<!-- InstanceEndEditable -->
<link rel="stylesheet" type="text/css" href="scouting.css"/>
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>

<body>
<div class="main">
<div class="top"><l>
<a class="navigation" href="./index.php">Home</a> | 
<a class="navigation" href="./input.php">Input Data</a> | 
<a class="navigation" href="./view.php">View Data</a> | 
<a class="navigation" href="./edit.php">Edit Data</a> | 
<a class="navigation" href="./predict.php">Match Predictor</a> | 
<script type="text/javascript">
function LoadLogin()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("content").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","login.php",true);
xmlhttp.send();
}
</script>
<?
if($logged_in)
{
	echo "<l> Welcome, " . $_SESSION['username'] . " </l>";
	echo "<a class=\"navigation\" href=\"./logout.php\">Log out</a>\n";
}
else
{
	echo "<l> You are not logged in. </l>";
	echo "<a class=\"navigation\" href=\"#\" onclick=\"LoadLogin()\">Log in</a>\n";
}

?>
</l>
</div>
<div id="content" class="content"><!-- InstanceBeginEditable name="Content" -->
<?
if($logged_in)
{
	if(isset($_POST['origmatch']))
	{
		$datatable=GetDataTable();
		$query="SELECT * FROM `".$datatable."` WHERE `Match`='".$_POST['origmatch']."' AND `Regional`='".$_SESSION['regional']."';";
		echo $query;
		$result=mysql_query($query);# or die(mysql_error());
		$cols=EnumerateColumnNames();
		$numcols=count($cols);
		$eventid=Geteventid(GetDataTable());
		#$q1="SELECT * FROM eventvars WHERE type='width' AND eventid='" . $eventid . "'";
		$q2="SELECT * FROM eventvars WHERE eventid='" . $eventid . "'";
		$res1=mysql_query($q2) or die(mysql_error());
		while($row=mysql_fetch_array($res1))
		{
			$formwidth[$row['fieldname']] = $row['width'];
			$maxvals[$row['fieldname']] = $row['maxval'];
		}
		echo "<form action='edit.php' method='post'>";
		echo "<table border='2' cellpadding='2' cellspacing='2'>";
		echo "<tr class=\"tableheader\">";
		for($i=1;$i<=$numcols;$i++)
		{
			$curcol=$cols[$i];
			echo "<td>".$curcol."</td>\n";
		}
		echo "<td>Delete Row</td>";
		echo "</tr>";
		$d=1;
		while($row=mysql_fetch_array($result))
		{
			echo "<tr>";
			for($i=1;$i<=$numcols;$i++)
			{
				if($d==1 && $cols[$i]=="Match")
				{
					echo "<td><input type='text' style=\"width: ".$formwidth[$cols[$i]]."px;\" name='newmatch' value='".$_POST['origmatch']."'/> </td>\n";
				}
				else
				{
					if($cols[$i]=="Match")
					{
						echo "<td></td>";
					}
					else
					{
						echo "<td> <input type='text' style=\"width: ".$formwidth[$cols[$i]]."px;\" name='".urlencode($cols[$i])."_".$d."' value='".$row[$cols[$i]]."'/> </td>\n";
					}
					#echo "<td><input type='text' name='".$cols[$i]."_".$d."' value='".$d."'/> </td>\n";
				}
			}
			echo "<td><input type='checkbox' name='delete_".$d."' value='1'></td>";
			echo "</tr>";
			$d++;
		}
		echo "</table>";
		echo "<input type=\"hidden\" name=\"originalmatch\" value=\"".$_POST['origmatch']."\" />";
		echo "<input type=\"hidden\" name=\"numrows\" value=\"".($d-1)."\" />";
		echo "<input type=\"submit\" value=\"Update Match\" />";
		echo "</form>";
	}
	elseif(isset($_POST['newmatch']))
	{
		$datatable=GetDataTable();
		$cols=EnumerateColumnNames();
		$numcols=count($cols);
		$numrows=$_POST['numrows'];
		$origmatch=$_POST['originalmatch'];
		$deleted=0;
		$inserted=0;
		$updated=0;
		for($i=1;$i<=$numrows;$i++)
		{
			if($_POST['delete_'.$i]==1)
			{
				$query="DELETE FROM `".$datatable."` WHERE ";
				for($j=1;$j<=$numcols;$j++)
				{
					if($cols[$j]=="Match")
					{
						$query=$query."`".$cols[$j]."`='".$origmatch."', ";
						continue;
					}
					if($_POST[urlencode($cols[$j]).'_'.$i]=='')
					{
					}
					else
					{
						$query=$query."`".$cols[$j]."`='".$_POST[urlencode($cols[$j]).'_'.$i];
						if($j==$numrows)
					{
						$query=$query;
					}
					else
					{
						$query=$query . ", ";
					}
					}
					//echo urlencode($cols[$j])."\n";
				}
				$query=$query." LIMIT 1";
				#echo $query."\n";
				mysql_query($query)or die(mysql_error());
				$deleted=$deleted+1;
			}
			else
			{
				$query="UPDATE `".$datatable."` SET ";
				for($j=1;$j<=$numcols;$j++)
				{
					if($cols[$j]=="Match")
					{
						$query=$query."`".$cols[$j]."`='".$_POST['newmatch']."', ";
						continue;
					}
					$query=$query."`".$cols[$j]."`='".$_POST[urlencode($cols[$j]).'_'.$i]."', ";
				}
				$query=$query."`Regional`='".$_SESSION['regional']."' WHERE `Match`='".$origmatch."' AND `Regional`='".$_SESSION['regional']."' AND `Team`='".$_POST['Team_'.$i]."' LIMIT 1;";
				mysql_query($query) or die(mysql_error());
				#echo $query."\n";
				$updated=$updated+1;
			}
		}
		echo "<p>Update completed!  ".$updated." Rows updated and ".$deleted." Rows deleted</p>";
	}
	else
	{
		?>
        <h1> Edit Match </h1>
        <form action="edit.php" method="post">
	    Enter a match number: <input type="text" name="origmatch">
	    <input type="Submit" value="Go"><br>
	    </form>
        <?
	}
}
else
{
	displayLogin();
}
mysql_close();
?>
<!-- InstanceEndEditable -->
</div>
</div>

</body>
<!-- InstanceEnd --></html>
